package de.clapp;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import de.clapp.menucard.MenucardFragment;
import de.clapp.musicbox.MusicFragment;
import de.clapp.toolkit.Tools;

public class ClubMainFragment extends Fragment
{
    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        setHasOptionsMenu(true);
        View view = inflater.inflate(R.layout.fragment_club, container, false);
        return view;
    }

    @SuppressLint("ValidFragment")
    @Override
    public void onResume()
    {
        super.onResume();
        CLAppModel.getInstance().setClubMainFragment(this);

        if (CLAppModel.getInstance().getCurrentClub() == null)
        {

            Tools.replaceFragment(this, R.id.clubLayout, new Fragment()
            {
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
                {
                    return inflater.inflate(R.layout.select_club, container, false);
                }
            });
        }
        else
        {
            Tools.replaceFragment(this, R.id.clubLayout, new MenucardFragment());
        }

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        menu.clear();
        MenuInflater menuInflater = getActivity().getMenuInflater();
        menuInflater.inflate(R.menu.menu_club, menu);
        menu.hasVisibleItems();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if (changeFragment(item.getItemId()))
            ;
        {
            Toast.makeText(getActivity(), getString(R.string.action_selected, item.getTitle()), Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }

    private boolean changeFragment(int itemId)
    {
        Fragment newFragment = null;
        switch (itemId)
        {
            case R.id.action_club_menu:
                newFragment = new MenucardFragment();
                break;
            case R.id.action_club_music:
                newFragment = new MusicFragment();
                break;
        }

        if (newFragment != null)
        {
            Tools.replaceFragment(this, R.id.clubLayout, newFragment);
            return true;
        }
        return false;
    }

    /**
     * Ersetzt das aktuelle Fragment
     * 
     * @param itemId
     *            z.B. R.id.action_club_menu:
     */
    public void setCurrentFragment(int itemId)
    {
        changeFragment(itemId);
    }

}
